/***1. 创建服务器 */
/*
var http=require('http'); // 1. http --- server ，request ，response

http.createServer(function (request,response) {

}).listen(3000,function () {
    console.log("the server is running on port 3000");
})*/

/*2.1 get - 获取 pathname 参数*/

/*var http=require('http')
var url = require('url');*/

/*
http.createServer(function (request,response) {
   // console.log(request);
      var urlObj = url.parse(request.url,true)
      console.log("pathname:"+urlObj.pathname);
      console.log("query:")
      console.log(urlObj.query);

      response.end();

}).listen(3000,function () {
    console.log("the server is running on port 3000");
})
*/


/**2.2 post - 获取 pathname 参数*/

var http=require('http')
var url = require('url');
var querystring = require('querystring')
var fs = require('fs');

http.createServer(function (request,response) {

     var urlObj = url.parse(request.url,true);
     var pathname = urlObj.pathname;
     console.log("pathname:"+urlObj.pathname);


     if(pathname=='/'){
         response.writeHead(200,{'Content-Type':'text/html;charset=utf8'})
         fs.readFile('template/login.html',function (err,data) {
             response.end(data.toString());
         })
     }


     if(pathname=='/login'){

         var postData='';
         request.on('data',function (chunk) {
             postData+=chunk;
         })
         request.on('end',function (error) {
             if(!error){
                 postData = querystring.parse(postData);
                 console.log(postData);
                 response.end(JSON.stringify(postData));

             }
         })
     }
}).listen(3000,function () {
    console.log("the server is running on port 3000");
})

